<!--
 * @Author: your name
 * @Date: 2021-12-29 21:52:08
 * @LastEditTime: 2022-01-06 11:05:43
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \am-ui\src\pages\am\device\List.vue
-->
<template>
  <div class="device">
    <!-- 按钮区域 -->
    <div class="btns">
      <el-form
        :inline="true"
        :model="params"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item>
          <el-input
            v-model="params.name"
            placeholder="请输入设备的关键字"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="params.engineer_id"
            clearable
            placeholder="请选择工程"
          >
            <el-option
              v-for="(item, index) in engineers"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="params.bind_status"
            clearable
            placeholder="请选择绑定状态"
          >
            <el-option label="已绑定" :value="1"></el-option>
            <el-option label="未绑定" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="params.online_status"
            clearable
            placeholder="请选择在线状态"
          >
            <el-option label="在线" :value="1"></el-option>
            <el-option label="离线" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearchHandler">查询</el-button>
        </el-form-item>
        <el-button
          @click="toAddHandler"
          style="float: right"
          type="primary"
          size="small"
          >添加</el-button
        >
      </el-form>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table size="small" :data="resData.list" style="width: 100%">
        <el-table-column
          label="序号"
          type="index"
          width="60px"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="serial_number"
          label="设备编号"
          width="100px"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="设备名称"
          align="center"
        ></el-table-column>
        <el-table-column label="绑定状态" width="80px" align="center">
          <template v-slot="scope">
            <el-tag size="mini" v-if="scope.row.bind_status == 1" type="success"
              >已绑定</el-tag
            >
            <el-tag size="mini" v-else type="danger">未绑定</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="online_status"
          label="在线状态"
          width="80px"
          align="center"
        >
          <template v-slot="scope">
            <el-tag
              size="mini"
              v-if="scope.row.online_status == 1"
              type="success"
              >在线</el-tag
            >
            <el-tag size="mini" v-else type="danger">离线</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="经度" width="160px" align="center">
          <template v-slot="scope">
            <div size="mini" v-if="scope.row.longitude == null">暂无</div>
            <div v-else>{{ scope.row.longitude }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="latitude"
          label="纬度"
          width="160px"
          align="center"
        >
          <template v-slot="scope">
            <div size="mini" v-if="scope.row.latitude == null">暂无</div>
            <div v-else>{{ scope.row.latitude }}</div>
          </template>
        </el-table-column>
        <el-table-column label="视频地址" width="200px" align="center">
          <template v-slot="scope">
            <el-tag size="mini" v-if="scope.row.video == null" type="info"
              >暂无</el-tag
            >
            <span v-else>{{ scope.row.video }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" align="center">
          <template v-slot="{ row }">
            <el-button @click="toEditHandler(row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              @click="toDeleteHandler(row.id)"
              type="text"
              size="small"
              style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件区域 -->
    <el-pagination
      background
      style="margin-top: 1em"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="parseInt(resData.pageSize)"
      layout="total, prev, pager, next, jumper"
      :total="parseInt(resData.total)"
    >
    </el-pagination>
    <!-- 模态框区域 -->
    <el-dialog
      @close="dialogCloseHandler"
      :title="title"
      :visible.sync="visible"
    >
      <el-form :model="form" :rules="rules" ref="deviceFrom">
        <el-form-item
          prop="name"
          label="设备名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="serial_number"
          label="设备编号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.serial_number" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmitHandler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// 导入工具插件
import { get, postJSON, del } from "@/utils/request.js";
export default {
  data() {
    return {
      resData: {},
      params: {
        page: 1,
        pageSize: 10,
      },
      visible: false, // 模态框显示与隐藏
      form: {},
      formLabelWidth: "120px", // 表单左侧label的宽度
      rules: {
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
        serial_number: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
        ],
      },
      title: "", // 模态框标题
      engineers: [],
    };
  },
  methods: {
    // 分页查询设备信息
    pageQueryDevices() {
      get("/device/pageQuery", this.params).then((res) => {
        // 将后台接口的响应结果，赋值给resData
        this.resData = res.data;
      });
    },
    // pageSize改变事件
    handleSizeChange(pageSize) {
      // 将分页插件最新的参数赋值给params
      this.params.pageSize = pageSize;
      // 调用查询设备的方法（数据重载）
      this.pageQueryDevices();
    },
    // page改变事件
    handleCurrentChange(page) {
      this.params.page = page;
      this.pageQueryDevices();
    },
    // 添加按钮点击事件
    toAddHandler() {
      // 给模态框标题赋值
      this.title = "添加设备";
      // 表单置空
      this.form = {};
      // 打开模态框
      this.visible = true;
    },
    // 表单提交事件
    toSubmitHandler() {
      this.$refs["deviceFrom"].validate((valid) => {
        if (valid) {
          postJSON("/device/saveOrUpdate", this.form).then((res) => {
            // 数据重载
            this.pageQueryDevices();
            // 成功提示
            this.$message({
              message: "操作成功",
              type: "success",
            });
            // 模态框关闭
            this.visible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 模态框关闭的回调
    dialogCloseHandler() {
      // 清空表单验证
      this.$refs["deviceFrom"].resetFields();
    },
    // 编辑按钮点击事件
    toEditHandler(row) {
      // 将当前行设备信息，赋值给表单
      this.form = {
        id: row.id,
        name: row.name,
        serial_number: row.serial_number,
      };
      // 给模态框标题赋值
      this.title = "修改设备";
      this.visible = true;
    },
    // 删除按钮点击事件
    toDeleteHandler(id) {
      this.$confirm("此操作将永久删除该设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 用户点击确认按钮，然后再做设备的删除
        del("/device/deleteById", { id }).then((res) => {
          // 数据重载
          this.pageQueryDevices();
          // 提示删除成功
          this.$message.success("删除成功");
        });
      });
    },
    // 查询所有工程信息
    findAllengineers() {
      get("/engineer/pageQuery", {
        page: 1,
        pageSize: 100,
      }).then((res) => {
        this.engineers = res.data.list;
      });
    },
    // 查询按钮点击事件
    toSearchHandler() {
      // 数据重载
      this.pageQueryDevices();
    },
  },
  // vue声明周期的钩子函数
  created() {
    // 页面加载，调用查询设备数据的方法
    this.pageQueryDevices();
    // 查询所有工程信息
    this.findAllengineers();
  },
};
</script>